package legumes.shared.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Serialized;

public class Store implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id private long id;
	private String storeName;
	//Store owner
	private String customerId;
	private boolean opened;
	private String message;
	private List<SellSession> sellSessionList;
	@Serialized private List<PlaceDelivery> placeDeliveryList;
	@Serialized private PaiementModes paiementModes;
	
	public Store(){
		sellSessionList = new ArrayList<SellSession>();
		placeDeliveryList = new ArrayList<PlaceDelivery>();
		paiementModes = new PaiementModes();
	}
	
	public PaiementModes getPaiementModes(){
		return paiementModes;
	}
	
	public void addPlaceDelivery(PlaceDelivery pd_){
		placeDeliveryList.add(pd_);
	}
	
	public void removePlaceDelivery(PlaceDelivery pd_){
		placeDeliveryList.remove(pd_);
	}
	
	public void addSellSession(SellSession ss_){
		sellSessionList.add(ss_);
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message_) {
		message = message_;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public boolean isOpened() {
		return opened;
	}

	public void setOpened(boolean isOpened) {
		this.opened = isOpened;
	}

	public SellSession findSellSession(long fromSellSessionId_) {
		SellSession ssFound = null;
		for(SellSession ss : sellSessionList){
			if(ss.getCreationDate() == fromSellSessionId_){
				ssFound = ss;
				break;
			}
		}
		return ssFound;
	}

	public String getStoreName() {
		return storeName;
	}

	public void setStoreName(String storeName) {
		this.storeName = storeName;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public List<SellSession> getSellSessionList() {
		return sellSessionList;
	}

	public void setSellSessionList(List<SellSession> sellSessionList) {
		this.sellSessionList = sellSessionList;
	}

	public List<PlaceDelivery> getPlaceDeliveryList() {
		return placeDeliveryList;
	}

	public void setPlaceDeliveryList(List<PlaceDelivery> placeDeliveryList) {
		this.placeDeliveryList = placeDeliveryList;
	}

	public boolean checkStoreCanDeliverInBounds(LatLng northEastBound_, LatLng southWestBound_){
		boolean isInBound = false;
		for(PlaceDelivery pl : getPlaceDeliveryList()){
			isInBound = pl.getLatLng().isInBound(northEastBound_, southWestBound_);
			if(!isInBound && pl instanceof HomePlaceDelivery){
				//Case of the circle
				int radius = ((HomePlaceDelivery)pl).getRadiusKm();
				isInBound =  pl.getLatLng().isCircleCoversBounds(northEastBound_, southWestBound_, radius);
			}
			
			if(isInBound){
				break;
			}
		}
		return isInBound;
	}
	
	public List<PlaceDelivery> getPlacesWhichCanDeliveredInBounds(LatLng northEastBound_, LatLng southWestBound_){
		List<PlaceDelivery> plList = new ArrayList<PlaceDelivery>();
		boolean isInBound = false;
		for(PlaceDelivery pl : getPlaceDeliveryList()){
			isInBound = pl.getLatLng().isInBound(northEastBound_, southWestBound_);
			if(!isInBound && pl instanceof HomePlaceDelivery){
				//Case of the circle
				int radius = ((HomePlaceDelivery)pl).getRadiusKm();
				isInBound =  pl.getLatLng().isCircleCoversBounds(northEastBound_, southWestBound_, radius);
			}
			
			if(isInBound){
				plList.add(pl);
				isInBound = false;
			}
		}
		return plList;
	}

	public void updatePlaceDelivery(PlaceDelivery oldPlaceDelivery_, PlaceDelivery newPlaceDelivery_) {
		if( getPlaceDeliveryList().contains(oldPlaceDelivery_) ){
			removePlaceDelivery(oldPlaceDelivery_);
		}
		addPlaceDelivery(newPlaceDelivery_);
	}
	
	public static Store retreiveStore(List<Store> storelist_, String storeToRetrieve_){
		Store resStore = null;
		int i =0;
		while(storeToRetrieve_!=null && i<storelist_.size() && !((resStore=storelist_.get(i)).getStoreName().equalsIgnoreCase(storeToRetrieve_) ) ){
			i++;
		}
		return resStore;
	}
}
